package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.NurseLeave;
import com.ruoyi.system.service.INurseLeaveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 护理人员请假Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/nurse/leave")
public class NurseLeaveController extends BaseController
{
    @Autowired
    private INurseLeaveService nurseLeaveService;

    @GetMapping("/list")
    public TableDataInfo list(NurseLeave nurseLeave)
    {
        // 护理人员只能查看自己的请假记录
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (!loginUser.getUser().isAdmin() && SecurityUtils.hasRole("nurse"))
        {
            nurseLeave.setNurseId(SecurityUtils.getUserId());
        }
        
        startPage();
        List<NurseLeave> list = nurseLeaveService.selectNurseLeaveList(nurseLeave);
        return getDataTable(list);
    }


    @Log(title = "护理人员请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NurseLeave nurseLeave)
    {
        List<NurseLeave> list = nurseLeaveService.selectNurseLeaveList(nurseLeave);
        ExcelUtil<NurseLeave> util = new ExcelUtil<NurseLeave>(NurseLeave.class);
        util.exportExcel(response, list, "护理人员请假数据");
    }


    @GetMapping(value = "/{leaveId}")
    public AjaxResult getInfo(@PathVariable("leaveId") Long leaveId)
    {
        return success(nurseLeaveService.selectNurseLeaveByLeaveId(leaveId));
    }

    @Log(title = "护理人员请假", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NurseLeave nurseLeave)
    {
        return toAjax(nurseLeaveService.insertNurseLeave(nurseLeave));
    }

    @Log(title = "护理人员请假", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NurseLeave nurseLeave)
    {
        return toAjax(nurseLeaveService.updateNurseLeave(nurseLeave));
    }

    @Log(title = "护理人员请假", businessType = BusinessType.DELETE)
    @DeleteMapping("/{leaveIds}")
    public AjaxResult remove(@PathVariable Long[] leaveIds)
    {
        return toAjax(nurseLeaveService.deleteNurseLeaveByLeaveIds(leaveIds));
    }
    

    @Log(title = "护理人员请假", businessType = BusinessType.AUDIT)
    @PutMapping("/audit/{leaveId}")
    public AjaxResult audit(@PathVariable Long leaveId, @RequestBody NurseLeave nurseLeave)
    {
        return toAjax(nurseLeaveService.auditNurseLeave(leaveId, nurseLeave.getStatus(), nurseLeave.getAuditRemark()));
    }
} 